package org.vaadinetherealintegration.client.example2;


import etrl.core.Geode;
import etrl.core.Group;
import etrl.core.Transform;
import etrl.vecmath.Vector3f;

public class Map
{
	private boolean[][] _cells;
	private Ground _ground;
	private int _size;
	private float _cellSize;
	private Group _graphRoot;
	
	String stone_wall = "/vaadinethereal/VAADIN/widgetsets/org.etherealengine.gwtcanvas.MyWidgetSet/stone_wall.bmp";
    String floor = "/vaadinethereal/VAADIN/widgetsets/org.etherealengine.gwtcanvas.MyWidgetSet/floor.jpg";
    
	public Map(int size, float cellSize)
	{
		_size = size;
		_cellSize = cellSize;
		_graphRoot = new Group();
		_graphRoot.setName("map");
		
		_cells = new boolean[size][size];
		for(int x = 0; x < _size; ++x)
			for(int y = 0; y < _size; ++y)
				_cells[x][y] = true;
		
		Transform groundTransform = new Transform();
		groundTransform.setName("ground transform");
		groundTransform.setPosition(new Vector3f(0.0f, 0.0f, 0.0f));
		groundTransform.setUniformScale(size * cellSize);
		groundTransform.rotate(new Vector3f(1.0f, 0.0f, 0.0f), -90.0f, true);
		
		_ground = new Ground();
		Geode groundGeode = new Geode();
		groundGeode.setName("ground");
		groundGeode.addDrawable(_ground);
		groundGeode.addTexture(floor, 20.f, 20.f);
		
		groundTransform.addChild(groundGeode);

		_graphRoot.addChild(groundTransform);
		_graphRoot.addTexture(stone_wall, 2.f, 2.f);

	}
	
	public void addBlock(int cx, int cy)
	{
		Block block = new Block();
		Geode blockGeode = new Geode();
		blockGeode.setName("block");
		blockGeode.addDrawable(block);
				
		float x = (cx + 0.5f) * _cellSize - (_size / 2.f) * _cellSize;
		float y = _cellSize / 2.f;
		float z = (cy + 0.5f) * _cellSize - (_size / 2.f) * _cellSize;
		
		Transform blockTransform = new Transform();
		blockTransform.setName("block transform");
		blockTransform.setPosition(new Vector3f(x, y, z));
		blockTransform.setUniformScale(_cellSize);
		blockTransform.setNodeCallback(new BlockCallback(blockTransform.getData()));
		
		blockTransform.addChild(blockGeode);
		
		_cells[cx][cy] = false;

		_graphRoot.addChild(blockTransform);
	}
	
	public Group getGraph()
	{
		return _graphRoot;
	}
}
